package com.le.tester.sevice.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.le.tester.dao.OgPostMapper;
import com.le.tester.dto.CommonDto;
import com.le.tester.entity.OgPost;
import com.le.tester.sevice.OGPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.testng.annotations.Test;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * createTime：2022/1/12 17:04
 * description：获取OGPost信息实现
 */
@Service
public class OGPostServiceImpl implements OGPostService {

    @Resource
    private OgPostMapper ogPostMapper;

    private Long postInfoCount = 0L;

    private static OgPostMapper ogPostMapperStatic;

    @Override
    public PageInfo<List<OgPost>> getPostInfo(CommonDto commonDto) {
        //1.使用PageHelper节省了一次select count(1) from tableName
        //分页的话还是解决不了数据量大的问题啊

//        PageHelper.startPage(commonDto.getPageNum(), commonDto.getPageSize());
//        List<OgPost> ogPosts = ogPostMapper.selectOgPostAll(commonDto);
//        return new PageInfo<>(Collections.singletonList(ogPosts));
        //this.multiThread(1);
        ExecutorService exec = Executors.newFixedThreadPool(10);
        exec.submit(new OgPostGetInfo());
        System.out.println(OgPostGetInfo.postInfoCount);
//        long t1 = System.currentTimeMillis();
//        this.asyncGetPostInfo();
//        long t2 = System.currentTimeMillis();

        return null;

        //2.开启多个线程分批的获取千万级别数据
    }

    //    @Async
//    public void multiThread(int ThreadNum) {
//        List<OgPost> ogPosts = ogPostMapper.selectOgPostAll(null);
//        System.out.println(ogPosts.toString());
//    }
    static class OgPostGetInfo implements Runnable {

        public static Long postInfoCount = 0L;

        @Override
        public void run() {
            System.out.println("it is run " + postInfoCount);
            postInfoCount = ogPostMapperStatic.selectOgPostAllCount();
            System.out.println("it is run " + postInfoCount);
        }
    }

    //@Async
//    public void asyncGetPostInfo() {
//        postInfoCount = ogPostMapper.selectOgPostAllCount();
//    }
    @PostConstruct
    public void init() {
        ogPostMapperStatic = ogPostMapper;
    }

}
